
$(function () {

	var lastIndex;
	$("#cityType").datagrid({
		fit:true, 
		nowrap:false, 
		striped:true, 
		collapsible:true, 
		sortOrder:"desc", 
		idField:"dictionaryID",
		url:js + "dict/viewCityType.action", 
		fitColumns:true, height:"auto", 
		pagination:true, 
		singleSelect:true,
		columns:[[
			{field:"dictionaryCode", title:"编号", width:120, sortable:true, editor:"text"},
			{field:"dictionaryName", title:"名称", width:120, sortable:true, editor:"text"}, 
			{field:"cityTypeLevel", title:"等级", width:120, sortable:true}
		]], 
		rownumbers:true, 
		toolbar:[
			{text:"添加", iconCls:"icon-add", handler:function () {
				var row = $('#cityType').datagrid('getSelected');
				if (row){
					var index = $('#cityType').datagrid('getRowIndex', row);
					$("#cityType").datagrid("endEdit", index);
					
				}
				var url= js + "dict/insertCityType.action";
				$.post(url, function(result){
					if(result.success){
						$("#cityType").datagrid("appendRow", {dictionaryCode:"",dictionaryName:"",dictionaryID:result.id,cityTypeLevel:$('#cityType').datagrid('getRows').length+1});
						lastIndex = $('#cityType').datagrid('getRows').length-1;
						$('#cityType').datagrid('selectRow', lastIndex);
						$('#cityType').datagrid('beginEdit', lastIndex);
					}
		 			
		 		},'json')
				
			}}
		], onBeforeLoad:function () {
		$(this).datagrid("rejectChanges");
	}, onClickRow:function (rowIndex) {
		if (lastIndex != rowIndex) {
			$("#cityType").datagrid("endEdit", lastIndex);
			$("#cityType").datagrid("beginEdit", rowIndex);
		}
		lastIndex = rowIndex;
	},
	onAfterEdit:function(rowIndex, rowData, changes){
		var hadChange=0;
		for(var name in changes){
			hadChange = 1 ;
			break;
		}
		if(hadChange){
		 	var url= js + "dict/updateCityType.action";
			$.post(url,rowData, function(data){
	 		
	 		},'json')
		}
	}});
});

